#include "exynos_4412.h"

#define TIME 1000000
void Init_ADC_LED(void);
void Delay(unsigned int time);

int main()
{
	unsigned int ADCvalue;
	//初始化ADC和LED并熄灭LED
	Init_ADC_LED();

	while (1)
	{
		//ADC转换器使能
		ADCCON = ADCCON | 1;
		//当转换结束时
		while (!(ADCCON & (1 << 15)));
		//读取ADC转换的结果
		ADCvalue = ADCDAT & 0xFFF;
		//转换结果为mv
		ADCvalue = ADCvalue * 0.44;
		printf("ADCvalue = %d\n", ADCvalue);
		//电压在1501mv~1800mv时，LED2、LED3、LED4、LED5点亮
		//电压在1001mv~1500mv时，LED2、LED3、LED4点亮
		//电压在501mv~1000mv时，LED2、LED3点亮
		//电压在0mv~500mv时，LED2闪烁
		switch (ADCvalue)
		{
		case 1501 ... 1800:
			//turn on LED2
			GPX2.DAT = GPX2.DAT | (1 << 7);
			//turn on LED3
			GPX1.DAT = GPX1.DAT | 1;
			//turn on LED4
			GPF3.DAT = GPF3.DAT | (1 << 4);
			//turn on LED5
			GPF3.DAT = GPF3.DAT | (1 << 5);
			break;
		case 1001 ... 1500:
			//turn off LED5
			GPF3.DAT = GPF3.DAT & (~(1 << 5));
			//turn on LED2
			GPX2.DAT = GPX2.DAT | (1 << 7);
			//turn on LED3
			GPX1.DAT = GPX1.DAT | 1;
			//turn on LED4
			GPF3.DAT = GPF3.DAT | (1 << 4);
			break;
		case 501 ... 1000:
			//turn off LED4
			GPF3.DAT = GPF3.DAT & (~(1 << 4));
			//turn off LED5
			GPF3.DAT = GPF3.DAT & (~(1 << 5));
			//turn on LED2
			GPX2.DAT = GPX2.DAT | (1 << 7);
			//turn on LED3
			GPX1.DAT = GPX1.DAT | 1;
			break;
		case 0 ... 500:
			//turn off LED3
			GPX1.DAT = GPX1.DAT & (~1);
			//turn off LED4
			GPF3.DAT = GPF3.DAT & (~(1 << 4));
			//turn off LED5
			GPF3.DAT = GPF3.DAT & (~(1 << 5));
			//turn on LED2
			GPX2.DAT = GPX2.DAT | (1 << 7);
			Delay(TIME);
			//turn off LED2
			GPX2.DAT = GPX2.DAT & (~(1 << 7));
			Delay(TIME);
			break;
		default:
			break;
		}

	}
	return 0;
}

void Init_ADC_LED(void)
{
	//选择ADC的精度为12bit
	ADCCON = ADCCON | (1 << 16);
	//分频器使能
	ADCCON = ADCCON | (1 << 14);
	//分频倍数设置成20倍
	ADCCON = ADCCON & (~(0xFF << 6)) | (19 << 6);
	//设置成正常模式
	ADCCON = ADCCON & (~(1 << 2));
	//关闭读操作启动ADC的模式
	ADCCON = ADCCON & (~(1 << 1));
	//选择第三通道
	ADCMUX = 3;

	//初始化LED2-5并全部熄灭
	//LED2 CONFIG
	GPX2.CON = GPX2.CON & (~(0xF << 28)) | (0x1 << 28);
	//LED3 CONFIG
	GPX1.CON = GPX1.CON & (~0xF) | 0x1;
	//LED4 CONFIG
	GPF3.CON = GPF3.CON & (~(0xF << 16)) | (0x1 << 16);
	//LED5 CONFIG
	GPF3.CON = GPF3.CON & (~(0xF << 20)) | (0x1 << 20);

	//turn off LED2
	GPX2.DAT = GPX2.DAT & (~(1 << 7));
	//turn off LED3
	GPX1.DAT = GPX1.DAT & (~1);
	//turn off LED4
	GPF3.DAT = GPF3.DAT & (~(1 << 4));
	//turn off LED5
	GPF3.DAT = GPF3.DAT & (~(1 << 5));
}

void Delay(unsigned int time)
{
	while (time--);
}
